var WxAutoImage = require('../../js/wxAutoImageCal.js');
var app = getApp();

Page({
  data: {
    userArray: [],
    userInfo: {},
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false
  },

  onLoad: function () {
    var that = this;
    var openid = wx.getStorageSync('openid');

    if (openid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else {
      var userInfo = {
        city: wx.getStorageSync('city'),
        province: wx.getStorageSync('province'),
        gender: wx.getStorageSync('gender'),
        nickName: wx.getStorageSync('nickName'),
        avatarUrl: wx.getStorageSync('avatarUrl'),
        openid: wx.getStorageSync('openid')
      }
      that.setData({
        userInfo: userInfo
      });
    }
    //获取微信绑定的其它用户
    that.getuserBindStu();
  },


  getuserBindStu: function () {
    var that = this;
    var openid = wx.getStorageSync('openid');
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/getuserBindStu.html',//请求地址
      data: {//发送给后台的数据
        openid: openid
      },
      header: {//请求头
        "Content-Type": "applciation/json"
      },
      method: "get",//get为默认方法/POST
      success: function (res) {
        if (res.data.length == 0) {
          //跳转到填写学生信息页，相当于注册与绑定
          wx.redirectTo({
            url: '../../pages/register/register'
          })
        } else {
          //选择登录那个学生
          that.setData({
            userArray: res.data
          });          
        }
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },

  buttona: function (e) {
    var index = e.target.dataset.index;
    var userArray = this.data.userArray;
  
    for (var i = 0; i < userArray.length;i++){
      if (index==i){
        wx.removeStorageSync('sid');
        wx.removeStorageSync('csid');
        wx.removeStorageSync('nj');
        wx.removeStorageSync('bj');
        wx.removeStorageSync('sfzh');
        wx.removeStorageSync('sname');

        wx.setStorageSync('sid', userArray[i].SID);//加入缓存
        wx.setStorageSync('csid', userArray[i].CSID);//加入缓存
        wx.setStorageSync('nj', userArray[i].NJ);//加入缓存
        wx.setStorageSync('bj', userArray[i].BJ);//加入缓存
        wx.setStorageSync('sfzh', userArray[i].SFZH);//加入缓存
        wx.setStorageSync('sname', userArray[i].SNAME);
      }
    }

    //相当于直接登录
    wx.switchTab({
      url: '../../pages/home/home'
    })

  },

})